package com.adgox.tiantianbiting.utils;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.adgox.tiantianbiting.R;
import com.adgox.tiantianbiting.bean.BaseResponseBean;
import com.adgox.tiantianbiting.bean.ShareUrlBean;
import com.adgox.tiantianbiting.bean.UserInfo;
import com.adgox.tiantianbiting.home.programDetail.ProgramDetailActivity;
import com.adgox.tiantianbiting.main.MainActivity;
import com.adgox.tiantianbiting.mine.tingCoinRecord.TingCoinRecordActivity;
import com.adgox.tiantianbiting.net.callback.JsonCallback;
import com.adgox.tiantianbiting.redpacket.share.share_for_free.ShareForFreeActivity;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.rae.swift.session.SessionManager;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class WebviewActivity extends AppCompatActivity {

    @BindView(R.id.wv)
    WebView wv;
    @BindView(R.id.pb)
    ProgressBar pb;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.ll_empty)
    LinearLayout llEmpty;
    private String url;
    private String title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        url = intent.getStringExtra("url");
        title = intent.getStringExtra("title");
        if (title != null) {
            tvTitle.setText(title);
        }
        init();
    }

    private void init() {
        wv.loadUrl(url);
        WebSettings settings = wv.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setLoadWithOverviewMode(true);
        settings.setBuiltInZoomControls(true);
        settings.setUseWideViewPort(true);
        settings.setSupportZoom(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        settings.setCacheMode(WebSettings.LOAD_DEFAULT);
        settings.setGeolocationEnabled(true);
        settings.setDomStorageEnabled(true);
        settings.setDatabaseEnabled(true);
        settings.setUseWideViewPort(true);
        settings.setAllowFileAccess(true);
        settings.setSupportZoom(true);
        settings.setLoadWithOverviewMode(true);
        settings.setPluginState(WebSettings.PluginState.ON);
        settings.setCacheMode(WebSettings.LOAD_NO_CACHE); // 不加载缓存内容

        wv.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return super.shouldOverrideUrlLoading(view, url);
            }
        });
        wv.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                if (newProgress == 100) {
                    llEmpty.setVisibility(View.GONE);
                    pb.setVisibility(View.GONE);
                } else {
                    pb.setVisibility(View.VISIBLE);
                    pb.setProgress(newProgress);
                    llEmpty.setVisibility(View.VISIBLE);
                }
            }
        });
        wv.addJavascriptInterface(new JiaoHu(), "android");
    }

    @OnClick(R.id.img_finish)
    public void onFinishClicked() {
        finish();
    }

    public class JiaoHu {
        @JavascriptInterface
        public void backHome() {

        }

        @JavascriptInterface
        public int getUserInfo() {
            UserInfo user = SessionManager.getDefault().getUser();
            return user.getId();
        }



        @JavascriptInterface
        public void toHome() {
            startActivity(new Intent(WebviewActivity.this, MainActivity.class).putExtra("flg","home"));
        }

        @JavascriptInterface
        public void toShare() {
            OkGo.<BaseResponseBean<ShareUrlBean>>get(ApiConstant.BASE_URL + ApiConstant.GET_SHARE_URL)
                    .params("flg", "go_download")
                    .execute(new JsonCallback<BaseResponseBean<ShareUrlBean>>() {
                        @Override
                        public void onSuccess(Response<BaseResponseBean<ShareUrlBean>> response) {
                            if ("0".equals(response.body().getCode()) && response.body().getData() != null) {
                                DialogUtils.showShareDialog(WebviewActivity.this, response.body().getData().getLocation());
                            } else {
                                Toast.makeText(WebviewActivity.this, "获取分享信息失败", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
        }

        @JavascriptInterface
        public void toRewordTbDetail() {
            startActivity(new Intent(WebviewActivity.this, TingCoinRecordActivity.class));
        }

        @JavascriptInterface
        public void toShareFreeListening() {
            startActivity(new Intent(WebviewActivity.this, ShareForFreeActivity.class));
        }

        @JavascriptInterface
        public void toProgramDetail(int programId) {
            Intent intent = new Intent(WebviewActivity.this, ProgramDetailActivity.class);
            intent.putExtra("programId", programId);
            startActivity(intent);
        }
    }
}
